/*   1:    */ package nl.siegmann.epublib.viewer;
/*   2:    */ 
/*   3:    */ import java.awt.Image;
/*   4:    */ import java.io.IOException;
/*   5:    */ import java.net.MalformedURLException;
/*   6:    */ import java.net.URL;
/*   7:    */ import java.util.Collection;
/*   8:    */ import java.util.Dictionary;
/*   9:    */ import java.util.Enumeration;
/*  10:    */ import java.util.HashMap;
/*  11:    */ import java.util.Map;
/*  12:    */ import java.util.Set;
/*  13:    */ import javax.imageio.ImageIO;
/*  14:    */ import javax.swing.text.html.HTMLDocument;
/*  15:    */ import nl.siegmann.epublib.browsersupport.Navigator;
/*  16:    */ import nl.siegmann.epublib.domain.Book;
/*  17:    */ import nl.siegmann.epublib.domain.Resource;
/*  18:    */ import nl.siegmann.epublib.domain.Resources;
/*  19:    */ import nl.siegmann.epublib.util.CollectionUtil;
/*  20:    */ import org.apache.commons.io.FilenameUtils;
/*  21:    */ import org.apache.commons.lang.StringUtils;
/*  22:    */ import org.slf4j.Logger;
/*  23:    */ import org.slf4j.LoggerFactory;
/*  24:    */ 
/*  25:    */ class ImageLoaderCache
/*  26:    */   extends Dictionary<String, Image>
/*  27:    */ {
/*  28:    */   public static final String IMAGE_URL_PREFIX = "http:/";
/*  29: 40 */   private static final Logger log = LoggerFactory.getLogger(ImageLoaderCache.class);
/*  30: 42 */   private Map<String, Image> cache = new HashMap();
/*  31:    */   private Book book;
/*  32: 44 */   private String currentFolder = "";
/*  33:    */   private Navigator navigator;
/*  34:    */   
/*  35:    */   public ImageLoaderCache(Navigator navigator)
/*  36:    */   {
/*  37: 48 */     this.navigator = navigator;
/*  38: 49 */     initBook(navigator.getBook());
/*  39:    */   }
/*  40:    */   
/*  41:    */   public void initBook(Book book)
/*  42:    */   {
/*  43: 53 */     if (book == null) {
/*  44: 54 */       return;
/*  45:    */     }
/*  46: 56 */     this.book = book;
/*  47: 57 */     this.cache.clear();
/*  48: 58 */     this.currentFolder = "";
/*  49:    */   }
/*  50:    */   
/*  51:    */   public void setContextResource(Resource resource)
/*  52:    */   {
/*  53: 62 */     if (resource == null) {
/*  54: 63 */       return;
/*  55:    */     }
/*  56: 65 */     if (StringUtils.isNotBlank(resource.getHref()))
/*  57:    */     {
/*  58: 66 */       int lastSlashPos = resource.getHref().lastIndexOf('/');
/*  59: 67 */       if (lastSlashPos >= 0) {
/*  60: 68 */         this.currentFolder = resource.getHref().substring(0, lastSlashPos + 1);
/*  61:    */       }
/*  62:    */     }
/*  63:    */   }
/*  64:    */   
/*  65:    */   public void initImageLoader(HTMLDocument document)
/*  66:    */   {
/*  67:    */     try
/*  68:    */     {
/*  69: 75 */       document.setBase(new URL("http:/"));
/*  70:    */     }
/*  71:    */     catch (MalformedURLException e)
/*  72:    */     {
/*  73: 77 */       log.error(e.getMessage());
/*  74:    */     }
/*  75: 79 */     setContextResource(this.navigator.getCurrentResource());
/*  76: 80 */     document.getDocumentProperties().put("imageCache", this);
/*  77:    */   }
/*  78:    */   
/*  79:    */   private String getResourceHref(String requestUrl)
/*  80:    */   {
/*  81: 85 */     String resourceHref = requestUrl.toString().substring("http:/".length());
/*  82: 86 */     resourceHref = this.currentFolder + resourceHref;
/*  83: 87 */     resourceHref = FilenameUtils.normalize(resourceHref);
/*  84: 88 */     return resourceHref;
/*  85:    */   }
/*  86:    */   
/*  87:    */   private Image createImage(Resource imageResource)
/*  88:    */   {
/*  89: 98 */     Image result = null;
/*  90:    */     try
/*  91:    */     {
/*  92:100 */       result = ImageIO.read(imageResource.getInputStream());
/*  93:    */     }
/*  94:    */     catch (IOException e)
/*  95:    */     {
/*  96:102 */       log.error(e.getMessage());
/*  97:    */     }
/*  98:104 */     return result;
/*  99:    */   }
/* 100:    */   
/* 101:    */   public Image get(Object key)
/* 102:    */   {
/* 103:108 */     if (this.book == null) {
/* 104:109 */       return null;
/* 105:    */     }
/* 106:112 */     String imageURL = key.toString();
/* 107:    */     
/* 108:    */ 
/* 109:115 */     Image result = (Image)this.cache.get(imageURL);
/* 110:116 */     if (result != null) {
/* 111:117 */       return result;
/* 112:    */     }
/* 113:121 */     String resourceHref = getResourceHref(imageURL);
/* 114:    */     
/* 115:    */ 
/* 116:124 */     Resource imageResource = this.book.getResources().getByHref(resourceHref);
/* 117:125 */     if (imageResource == null) {
/* 118:126 */       return result;
/* 119:    */     }
/* 120:130 */     result = createImage(imageResource);
/* 121:131 */     if (result != null) {
/* 122:132 */       this.cache.put(imageURL.toString(), result);
/* 123:    */     }
/* 124:135 */     return result;
/* 125:    */   }
/* 126:    */   
/* 127:    */   public int size()
/* 128:    */   {
/* 129:139 */     return this.cache.size();
/* 130:    */   }
/* 131:    */   
/* 132:    */   public boolean isEmpty()
/* 133:    */   {
/* 134:143 */     return this.cache.isEmpty();
/* 135:    */   }
/* 136:    */   
/* 137:    */   public Enumeration<String> keys()
/* 138:    */   {
/* 139:147 */     return CollectionUtil.createEnumerationFromIterator(this.cache.keySet().iterator());
/* 140:    */   }
/* 141:    */   
/* 142:    */   public Enumeration<Image> elements()
/* 143:    */   {
/* 144:151 */     return CollectionUtil.createEnumerationFromIterator(this.cache.values().iterator());
/* 145:    */   }
/* 146:    */   
/* 147:    */   public Image put(String key, Image value)
/* 148:    */   {
/* 149:155 */     return (Image)this.cache.put(key.toString(), value);
/* 150:    */   }
/* 151:    */   
/* 152:    */   public Image remove(Object key)
/* 153:    */   {
/* 154:159 */     return (Image)this.cache.remove(key);
/* 155:    */   }
/* 156:    */   
/* 157:    */   public void clear()
/* 158:    */   {
/* 159:166 */     this.cache.clear();
/* 160:    */   }
/* 161:    */   
/* 162:    */   public String toString()
/* 163:    */   {
/* 164:170 */     return this.cache.toString();
/* 165:    */   }
/* 166:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.viewer.ImageLoaderCache
 * JD-Core Version:    0.7.0.1
 */